package com.delos.rpc.error;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * RPC异常封装
 *
 * @author zouyaowen
 * @date 2020-05-17
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RpcException extends RuntimeException implements Serializable {

    private Integer code;
    private String message;
    private String requestIp;
    private String responseIp;
    private String methodName;
    private Object[] arguments;

    public RpcException(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public String getMessage() {
        return "code:" + this.code + "," + "message:" + this.message + "," + "requestIp:" + this.requestIp + "," +
                "responseIp:" + this.responseIp + "," + "methodName:" + this.methodName + "," + getArgumentsData();
    }

    /**
     * 获取所有的参数
     *
     * @return 参数
     */
    private String getArgumentsData() {
        if (arguments == null || arguments.length == 0) {
            return "";
        }
        StringBuilder res = null;
        for (int i = 0; i < arguments.length; i++) {
            if (res != null) {
                res = new StringBuilder((String) arguments[i]);
            } else {
                res.append((String) arguments[i]);
            }
        }
        return res.toString();
    }
}

